package com.flyduck.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.common.util.IdUtil;
import com.flyduck.common.util.StringUtil;
import com.flyduck.common.util.TokenUtil;
import com.flyduck.common.vo.PageResult;
import com.flyduck.system.dao.SysPostDao;
import com.flyduck.system.enums.StatusEnum;
import com.flyduck.system.vo.post.*;
import com.flyduck.system.vo.post.trans.*;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flyduck.system.entity.SysPostEntity;

import java.util.Date;
import java.util.List;

@Service
public class SysPostService {


    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private SysPostDao sysPostDao;

    /**
     * 获取职位下拉(未删除、并且启用)
     * @return
     */
    public List<SysPostSelectRespVO> getPostSelect() {
        QueryWrapper<SysPostEntity> wrapper = getPostQueryWrapper();
        return Lists.transform(sysPostDao.list(wrapper),new SysPostEntityToSysPostSelectRespVO());
    }

    private QueryWrapper<SysPostEntity> getPostQueryWrapper() {
        QueryWrapper<SysPostEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusEnum.ENABLE.getValue());
        return wrapper;
    }

    /**
     * 获取职位分页
     * @param reqVo
     * @param page
     * @param size
     * @return
     */
    public PageResult<SysPostRespVO> getPostPageList(SysPostReqVO reqVo, Integer page, Integer size) {
        //************************* 1.构建分页条件 *************************
        Page<SysPostEntity> postEntityPage = new Page<>(page, size);
        //************************* 2.构建查询条件 *************************
        QueryWrapper<SysPostEntity> wrapper = getPostQueryWrapper(reqVo);
        //************************* 3.查询 *************************
        IPage<SysPostEntity> postEntityIPage = sysPostDao.page(postEntityPage, wrapper);
        //************************* 4.结果返回转换 *************************
        List<SysPostRespVO> respVOList = Lists.transform(postEntityIPage.getRecords(), new SysPostEntityToSysPostRespVO());
        return PageResult.ofPages(respVOList, postEntityIPage);
    }

    private QueryWrapper<SysPostEntity> getPostQueryWrapper(SysPostReqVO reqVo) {
        QueryWrapper<SysPostEntity> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotBlank(reqVo.getPostName())) {
            wrapper.like("post_name", reqVo.getPostName());
        }
        return wrapper;
    }

    /**
     * 获取职位详情
     * @param id
     * @return
     */
    public SysPostDetailRespVO getPostDetail(Long id) {
        SysPostEntity sysPostEntity = sysPostDao.getById(id);
        return new SysPostEntityToSysPostDetailRespVO().apply(sysPostEntity);
    }

    /**
     * 新增职位
     * @param reqVo
     * @return
     */
    public boolean addPost(SysPostAddReqVO reqVo) {
        SysPostEntity postEntity = new SysPostAddReqVOToSysPostEntity().apply(reqVo);
        postEntity.setId(IdUtil.getId());
        postEntity.setCreateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        postEntity.setCreateTime(new Date());
        postEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        postEntity.setUpdateTime(new Date());
        return sysPostDao.save(postEntity);
    }

    /**
     * 修改职位
     * @param reqVo
     * @return
     */
    public boolean updatePost(SysPostUpdateReqVO reqVo) {
        SysPostEntity postEntity = new SysPostUpdateReqVOToSysPostEntity().apply(reqVo);
        postEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        postEntity.setUpdateTime(new Date());
        return sysPostDao.updateById(postEntity);
    }

    /**
     * 删除职位
     * @param id
     * @return
     */
    public boolean deletePost(Long id) {
        return sysPostDao.removeById(id);
    }


}
